<?php
namespace User\Controller;

use Common\Controller\AdminbaseController;

class CollectionController extends AdminbaseController {
    public function __construct(){
        parent::__construct();
        $this->model = M( "collection_type", 'system_', C( 'DB_CONFIG2' ) );
        $this->jum_url = 'http://127.0.0.1/';
    }
    /*
     * 客户反映类型列表
     */
    public function index(){
        $where = [];
        $request = I( 'request.' );
        if(!empty( $request['name'] )) {
            $where['name'] = [ 'like', "%{$request['name']}%" ];
        }
        $count = $this->model->where( $where )->count();
        $page = $this->page( $count, 20 );
        $list = $this->model->field('id,name,crtime,is_true')
            ->where( $where )
            ->order( "crtime desc,id asc" )
            ->limit( $page->firstRow . ',' . $page->listRows )
            ->select();
        $this->assign( 'list', $list );
        $this->assign( "page", $page->show( 'Admin' ) );

        $this->display();
    }
    /*
     * 编辑
     */
    public function set(){
        $request = I( 'request.' );
        if(!empty($request['id'])){
            $list=$this->model->field('id,name')->where(['id'=>$request['id']])->find();
        }else{
            $list=[];
        }
        $this->assign( 'list', $list );
        $this->display();
    }
    public function set_act(){
        $request = I( 'request.options' );
        if(empty($request['name'])){
            $this->error( "请输入类型名称！" );
        }
        if(!empty($request['id'])){
            $data=[
                'name'=>$request['name'],
                'crtime'=>date('Y-m-d H:i:s'),
            ];
            $this->model->where(['id'=>$request['id']])->save($data);
            $this->success( "编辑成功！", U( "collection/index" ) );
        }else{
            $data=[
                'name'=>$request['name'],
                'crtime'=>date('Y-m-d H:i:s'),
            ];
            $this->model->add($data);
            $this->success( "添加成功！", U( "collection/index" ) );
        }
    }
    /*
     * 是否启用
     */
    public function check_type(){
        $request = I( 'request.' );
        if(empty($request['id'])){
            $this->error( "请刷新重试！" );
        }
        $coll=$this->model->field('id,is_true')->where(['id'=>$request['id']])->find();
        if(empty($coll['id'])){
            $this->error( "没找到该类型,请刷新重试！" );
        }
        if($coll['is_true']==1){
            $type=0;
            $msg='关闭成功!';
        }else{
            $type=1;
            $msg='启用成功!';
        }
        $this->model->where(['id'=>$request['id']])->save(['is_true'=>$type]);
        $this->success( $msg, U( "collection/index" ) );
    }
}
